﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.DBM;
using System.Globalization; 

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class DC_InvestFact : System.Web.UI.UserControl
    {


     
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {

          
            

        }



        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

  
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
       //     BindCombo();
            if (!IsUserControlPostBack)
            {
                BindRadio();
     
                this.ViewState.Add("IsUserControlPostBack", true);

            }



            if (DataItem != null)
            {
                //   cbGender.ListGenderComboBox();

                BindCombo();
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
               
                    lblInvSeq.Text   = DataBinder.Eval(DataItem, "inv_seq").ToString();

                    DateTime date_Tmp;
                    
                    date_Tmp = Convert.ToDateTime(DataBinder.Eval(DataItem, "inv_date").ToString()); 

                    string dateTmp;

                    dateTmp = date_Tmp.ToString("dd/MM/yyyy", new CultureInfo("th-TH"))  ;
                    txtInvDate.SetValue(dateTmp);  




    txtInvRefNo.Text = DataBinder.Eval(DataItem, "inv_ref_no").ToString();
    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
    txtCrimeDescr.Text = DataBinder.Eval(DataItem, "crd_descr").ToString();
    txtInvName.Text = DataBinder.Eval(DataItem, "inv_name").ToString();
    RadComboBox1.SelectedValue = DataBinder.Eval(DataItem, "crd_seq").ToString();
    radStatus.SelectedValue = DataBinder.Eval(DataItem, "stat_seq").ToString();


                }


             

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }


       
      
        }


        private void BindRadio()
        {
            InvestFactBLL obj = new InvestFactBLL();
            DataTable dt;
            dt = obj.SelectStatus();

            if (dt != null && dt.Rows.Count > 0)
            {
                radStatus.Items.Clear();
                radStatus.DataSource = dt;
                radStatus.DataTextField = "stat_name";
                radStatus.DataValueField = "stat_seq";
                //foreach (DataRow cur in dt.Rows)
                //{
                //    ListItem Li = new ListItem();
                //    Li.Text = cur["record_status_name"].ToString();// + " " + ((DateTime)cur["act_start_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH")) + " ถึง " + ((DateTime)cur["act_end_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                //    Li.Value = cur["record_status"].ToString();
                //    radStatus.Items.Add(Li);
                //}

                radStatus.DataBind();
             //   radStatus.SelectedIndex = 0;
            }
            else
            {
                radStatus.Items.Clear();
            }




        }

        private void BindCombo()
        {
            try
            {
                InvestFactBLL obj = new InvestFactBLL();

                DataTable dt;
                dt = obj.SelectComboCrime();
                RadComboBox1.DataValueField  = "crd_seq";
                RadComboBox1.DataTextField = "crd_name";
                RadComboBox1.DataSource = dt;
                RadComboBox1.DataBind();  
 

            }
            catch (Exception ex)
            {

            }

        }










        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            string invDate, invRefNO,invName,remarks,crimeDesc;
            int crd_seq,stat;
            invDate = txtInvDate.GetStringValue();
            //invDate = Utils.Date.ToEngFormatDate(invDate);     
            invRefNO = txtInvRefNo.Text ;
            remarks = txtRemarks.Text;
            crimeDesc = txtCrimeDescr.Text;
            invName = txtInvName.Text;
        

            stat = int.Parse(radStatus.SelectedValue);   
            crd_seq = int.Parse(RadComboBox1.SelectedValue);
            InvestFactBLL obj = new InvestFactBLL();

            obj.insert(invName, invRefNO, invDate, remarks, crimeDesc, crd_seq, stat, 1);        
         
 
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            string invDate, invRefNO, invName, remarks, crimeDesc;
            int crd_seq, stat,dataKey;
            invDate = txtInvDate.GetStringValue() ;
            invRefNO = txtInvRefNo.Text;
            remarks = txtRemarks.Text;
            crimeDesc = txtCrimeDescr.Text;
            invName = txtInvName.Text;

            stat = int.Parse(radStatus.SelectedValue);
            crd_seq = int.Parse(RadComboBox1.SelectedValue);
            InvestFactBLL obj = new InvestFactBLL();
            dataKey = int.Parse(lblInvSeq.Text);


            obj.Update(dataKey, invName, invRefNO, invDate, remarks, crimeDesc, crd_seq, stat, 1);         



        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void RadTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}